<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Action\ConfirmAction;
use App\Models\Business;
use App\Models\BusinessApply;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;

class BusinessApplyController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('厂商入驻申请');
            $content->description('');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('厂商入驻处理');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('新增');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(BusinessApply::class, function (Grid $grid) {

            $grid->id('ID')->sortable();

            $grid->name('厂商名称');

            $grid->licence_img('营业执照')->fancyImage('',100,100);
            $grid->phone('联系方式');
            $grid->status('状态')->display(function ($value) {
                if ($value == 1) {
                    return "<span style='color:red'>未处理</span>";
                } else if ($value == 2) {
                    return "<span style='color:green'>已处理</span>";
                } else if ($value == 3) {
                    return "<span style='color:grey'>已拒绝</span>";;
                }
            });
            //操作
            $grid->actions(function ($actions) {
                $data = $actions->row;
                if($data['status']==1){
                    $actions->append(new ConfirmAction($actions->getKey(), 'business/apply/handle/' . $actions->getKey() . '/2', 'business/apply/handle/' . $actions->getKey() . '/3', '您确定同意商家入驻？', '您确定拒绝该商家的入驻？'));
                }
                $actions->disableDelete();
                $actions->disableEdit();
            });

            //关掉批量删除
            $grid->tools(function ($tools) {
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });

            $grid->filter(function ($filter) {
                // 禁用id查询框
                $filter->disableIdFilter();
                //分类查询
                $filter->equal('status', '分类')->select(['1'=>'未处理','2'=>'已处理','3'=>'已拒绝']);
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(BusinessApply::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    protected function handleApply($id, $status)
    {
        if ($status == 2) {
            $apply = BusinessApply::find($id)->toArray();
            $info = [];
            $info['name'] = $apply['name'];
            $info['phone'] = $apply['phone'];
            $info['licence_img'] = $apply['licence_img'];
            $info['user_id'] = $apply['user_id'];
            $info['status'] = 1;
            DB::beginTransaction();
            //判断是否有相同厂商
            $res = Business::where(['user_id' => $info['user_id']])->count();
            if ($res > 0) {
                return ['code' => -1, 'msg' => '该用户已经创建厂商'];
            }

            $res = BusinessApply::find($id)->update(['status' => $status]);
            if (!$res) {
                DB::rollBack();
                return ['code' => -1, 'msg' => '操作失败'];
            }
            $res = Business::create($info);
            if (!$res) {
                DB::rollBack();
                return ['code' => -1, 'msg' => '操作失败'];

            }
            DB::commit();
            return ['code' => 0, 'msg' => '操作成功'];
        } else if ($status == 3) {
            $res = BusinessApply::find($id)->update(['status' => $status]);
            if (!$res) {
                return ['code' => -1, 'msg' => '操作失败'];
            }
            return ['code' => 0, 'msg' => '操作成功'];
        }


    }
}
